<!--
 * @Description:
 * @Author: Morgan
 * @Date: 2021-06-24 17:07:01
 * @LastEditors: Morgan
 * @LastEditTime: 2021-07-02 17:15:08
-->
<template>
  <!-- 资讯分类管理 -->
  <div>
    <div class="addbtn">
      <el-button class="btn" type="primary" @click="toAdd">添加</el-button>
    </div>
    <div>
      <!-- {{ tableData }} -->
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        style="width: 100%"
      >
        <el-table-column label="序号" type="index" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="no" label="排序号" />
        <el-table-column prop="description" label="描述" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              class="green_info"
              @click="toEdit(scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              size="small"
              class="red_info"
              @click="toDelete(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <!-- {{ form }} -->
      <el-form ref="ruleForm" :model="form" :rules="rules">
        <el-form-item
          label="栏目名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="序号" :label-width="formLabelWidth">
          <el-input v-model="form.no" autocomplete="off" />
        </el-form-item>
        <el-form-item label="栏目介绍" :label-width="formLabelWidth">
          <el-input v-model="form.description" type="textarea" :rows="2" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { findAll, saveOrUpdate, deleteById } from '@/api/infoCategory'

export default {
  data() {
    return {
      // 表格展示的数据
      tableData: [],
      loading: false,
      // 表单数据
      form: {},
      visible: false,
      formLabelWidth: '80px',
      title: '',
      rules: {
        name: [
          {
            required: true,
            message: '请输入产品名称',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {},
  created() {
    // 发请求
    this.findAllCategoryData()
  },
  mounted() {},
  methods: {
    // 获取资讯分类数据
    async findAllCategoryData() {
      this.loading = true
      const res = await findAll()
      this.tableData = res.data
      // 加载完毕
      this.loading = false
      // console.log(res.data.list.name);
    },
    // 添加
    toAdd() {
      this.findAllCategoryData()
      // 清空表单数据
      this.form = {}
      this.visible = true
      this.title = '新增栏目信息'
    },
    // 模态框确定按钮事件处理程序
    toSave() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          console.log('校验通过')
          const res = await saveOrUpdate(this.form)
          // 提示用户保存成功，刷新表格数据，关闭模态框
          this.$notify.success({
            title: '成功',
            message: res.success
          })
          this.findAllCategoryData()
          // this.toClose();
          this.visible = false
        } else {
          return false
        }
      })
    },
    // 重置表单验证
    toClose() {
      // 重置表单验证
      this.$refs.ruleForm.resetFields()
    },
    // 编辑按钮事件处理程序
    toEdit(row) {
      this.findAllCategoryData()
      // 设置表单数据
      this.form = { ...row }
      this.title = '编辑栏目消息'
      this.visible = true
    },
    // 删除按钮事件处理程序
    toDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteById({ id })
        this.$notify.success({
          title: '成功',
          message: res.success
        })
        this.findAllCategoryData()
      }).catch(() => {
        this.$notify.info({
          title: '取消',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style scoped>
.addbtn{
  margin-bottom: 20px;
}
</style>
